//
//  CheckResultViewController.m
//  checkApp
//
//  Created by ma on 2017/6/13.
//  Copyright © 2017年 ma. All rights reserved.
//

#import "CheckResultViewController.h"
#import "ResultView.h"

@interface CheckResultViewController ()

@property (nonatomic,strong)CheckCodeModel *checkModel;
@property (nonatomic,strong)UIImageView *topImage;
@property (nonatomic,strong)UILabel *topLabel;
@property (nonatomic,strong)UIView *topView;

@property (nonatomic,strong)ResultView *resultView;
@property (nonatomic,strong)UILabel *reasonLabel;

@end

@implementation CheckResultViewController
{
    NSArray *picArr;
    NSArray *resultArr;
    NSArray *colorArr;
    UITableView *tableView;
}

- (UIImageView *)topImage {
    if (_topImage == nil) {
        _topImage = [[UIImageView alloc] init];
        [_topView addSubview:_topImage];
        [_topImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(20);
            make.centerX.equalTo(self.view).offset(10*boringScale);
            make.size.mas_equalTo(CGSizeMake(200*boringScale, 200*boringScale));
        }];
    }
    return _topImage;
}

- (UILabel *)topLabel {
    if (_topLabel == nil) {
        _topLabel = [UILabel labelWithFrame:CGRectZero text:@"" font:20*boringScale textColor:color(0,0,0,0.9)];
        _topLabel.numberOfLines = 0;
        _topLabel.font = [UIFont fontWithName:@"Georgia-BoldItalic" size:23*boringScale];
        _topLabel.textColor = color(140, 219, 86, 0.8);
        [_topView addSubview:_topLabel];
        [_topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_topImage.mas_bottom).offset(10*boringScale);
            make.centerX.equalTo(self.view).offset(10*boringScale);
            make.height.mas_equalTo(50*boringScale);
        }];
        UIView *sepView = [[UIView alloc] init];
        sepView.backgroundColor = sepline_color;
        [_topView addSubview:sepView];
        [sepView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.mas_equalTo(0);
            make.trailing.mas_equalTo(0);
            make.height.mas_equalTo(1);
            make.top.equalTo(_topLabel.mas_bottom).offset(10*boringScale);
        }];
    }
    return _topLabel;
}

- (UIView *)resultView {
    if (_resultView == nil) {
        _resultView = [[ResultView alloc] initWithFrame:CGRectZero];
        _resultView.hidden = YES;
        [_topView addSubview:_resultView];
        [_resultView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.mas_equalTo(0);
            make.trailing.mas_equalTo(0);
            make.top.equalTo(_topLabel.mas_bottom).offset(20*boringScale);
        }];
    }
    return _resultView;
}

- (UILabel *)reasonLabel {
    if (_reasonLabel == nil) {
        _reasonLabel = [UILabel labelWithFrame:CGRectZero text:@"" font:20*boringScale textColor:color(0,0,0,0.9)];
        _reasonLabel.numberOfLines = 0;
        _reasonLabel.font = [UIFont fontWithName:@"Georgia-BoldItalic" size:23*boringScale];
        _reasonLabel.textColor = color(140, 219, 86, 0.8);
        _reasonLabel.hidden = YES;
        [_topView addSubview:_reasonLabel];
        [_reasonLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_topLabel.mas_bottom).offset(20*boringScale);
            make.centerX.equalTo(self.view);
        }];
    }
    return _reasonLabel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"检验结果";
    picArr = @[@"success",@"haveUse",@"forbid",@"error"];
    resultArr = @[@"二维码验证成功",@"二维码已使用",@"二维码无效",@"二维码错误"];
    colorArr = @[color(140, 219, 86, 0.8),blue_color,color(254, 213, 68, 0.8),red_color];
    
    [self initView];
    
    [self initData];
}

- (void)initView {
    
    tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, screen_width, screen_height) style:UITableViewStylePlain];
    [self.view addSubview:tableView];
    tableView.tableFooterView = [[UIView alloc] init];
    _topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screen_width, 0)];
    tableView.tableHeaderView = _topView;
    _topView.height = 450*boringScale+50*boringScale;
    
    [self topImage];
    [self topLabel];
    [self resultView];
    
}

- (void)initData {
    //获取uid和qrCode
//    NSArray *arr = [self.scanResult componentsSeparatedByString:@","];
    //获取设备id
//    NSString *identifierForVendor = [[UIDevice currentDevice].identifierForVendor UUIDString];
    NSDictionary *params = @{@"machineCode":@"loger168",
                             @"projectId":@"5",
                             @"showInfoId":@"48",
                             @"uid":@"24",
                             @"qrCode":@"0a1b0cfa90bd4084913636a0e84de1bc"};
    NSDictionary *style = @{@"red":red_color};
    [NetRequest postDataWithUrlString:CHECK_CODE_URL withParams:params success:^(id data) {
        
        NSLog(@"data is %@",data);
        
        _checkModel = [[CheckCodeModel alloc] initWithDataDic:data[@"data"]];
        
        _topLabel.text = resultArr[[_checkModel.checkStatus intValue]-1];
        _topLabel.textColor = colorArr[[_checkModel.checkStatus intValue]-1];
        _topImage.image = [UIImage imageNamed:picArr[[_checkModel.checkStatus intValue]-1]];
        
        _topView.height = 450*boringScale+40*boringScale+40*boringScale*_checkModel.usedTime.count;
        tableView.tableHeaderView = _topView;
        
        if ([_checkModel.checkStatus intValue] == 1 || [_checkModel.checkStatus intValue] == 2) {
            _resultView.hidden = NO;
            _reasonLabel.hidden = YES;
            _resultView.checkModel = _checkModel;
        }else {
            _resultView.hidden = YES;
            _reasonLabel.hidden = NO;
            
            _reasonLabel.attributedText = [[NSString stringWithFormat:@"<red>扫描结果：</red>%@",_checkModel.errorShow] attributedStringWithStyleBook:style];
        }
        
    } fail:^(NSString *errorDes) {
        NSLog(@"数据请求失败");
    } refresh:^{
        [self initData];
    }];
}

- (void)showFrameAnimationBubble:(UIButton *)sender {
    LKBubbleInfo *frameInfo = [[LKBubbleInfo alloc] init];
    NSMutableArray<UIImage *> *icons = [[NSMutableArray alloc] init];
    for (int i = 1 ; i <= 8; i ++) {
        [icons addObject: [UIImage imageNamed: [NSString stringWithFormat: @"lkbubble%d.jpg" , i]]];
    }
    frameInfo.iconArray = icons;
    // 在数组中依次放入多张图片即可实现多图循环播放
    frameInfo.backgroundColor = [UIColor colorWithRed: 238 / 255.0 green:238 / 255.0 blue:238 / 255.0 alpha:1];
    // 动画的帧动画播放间隔
    frameInfo.frameAnimationTime = 0.15;
    frameInfo.title = @"正在加载中...";
    frameInfo.titleColor = [UIColor darkGrayColor];
    [[LKBubbleView defaultBubbleView] showWithInfo: frameInfo];
}

- (void)hideBubbleFrame {
    [[LKBubbleView defaultBubbleView] hideBubble];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
